//
//  WPTabbar.m
//  01 微博
//
//  Created by zhoup on 16/5/9.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "WPTabbar.h"
#import "UIView+Frame.h"

#import "ComposeViewController.h"

@interface WPTabbar()

//加号按钮
@property(nonatomic,strong)UIButton *addBtn;

@end

@implementation WPTabbar

-(UIButton *)addBtn
{
    if (!_addBtn) {
        
        // 1.创建加号按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        
        // 2.设置按钮的image图片
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //发送微博
        [btn addTarget:self action:@selector(compose:) forControlEvents:UIControlEventTouchUpInside];
        
        // 3.设置按钮的背景Image图片
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        _addBtn = btn;
        [self addSubview:_addBtn];
    }
    return _addBtn;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    int btnIndex = 0;
    
    CGFloat tabbarButtonW =  self.bounds.size.width / 5.0;
    CGFloat tabbarButtonH = self.bounds.size.height;
    //调整4各按钮的坐标
    for (int i = 0; i < self.subviews.count; i++) {
        UIView *child = self.subviews[i];
        if ([child isKindOfClass:[UIControl class]] && ![child isKindOfClass:[UIButton class]]) {
            
            if (btnIndex == 2) {
                btnIndex ++;
            }
            CGFloat tabbarButtonX = btnIndex * tabbarButtonW;
            child.frame =  CGRectMake(tabbarButtonX, 0, tabbarButtonW, tabbarButtonH);
            btnIndex ++;
        }
    }
    
    // 5.设置加号按钮的frame
    self.addBtn.size = self.addBtn.currentBackgroundImage.size;
    self.addBtn.center = CGPointMake(self.width * 0.5, self.height * 0.5);

    DDLogInfo(@"%@",NSStringFromCGRect(self.addBtn.frame));
    DDLogInfo(@"%@",self.subviews);
    // 一定不要使用self.center self.center是相对父控件的坐标
//    self.addBtn.center =  self.center;

}

#pragma mark - compose
//除了代理还要一种方法
-(void)compose:(UIButton *)sender
{
//    ComposeViewController *vc =[[ComposeViewController alloc] init];
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Compose" bundle:nil];
    
    UINavigationController *nav = [sb instantiateInitialViewController];
    
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    [rootVc presentViewController:nav animated:YES
                       completion:^{
                           
                       }];
}

@end
